define(function (require,exports,module) {
    var angular = require('angular');

    var material = angular.module('uploader',[]);

    require('angular-file-upload');

    material.service('uploaderService',[
        'FileUploader',
        'apife',
        'apibe',
        function (FileUploader,apife,apibe) {
            this.createFileUploader = function (options) {
                return new FileUploader({
                    url: apife + apibe + options.api,
                    filters: options.filters || [],
                    alias: 'file',
                    formData: options.data,
                    autoUpload: true,
                    removeAfterUpload: true,
                    onBeforeUploadItem : function(fileItem){
                        options.onBeforeUploadItem && options.onBeforeUploadItem(fileItem);
                    },
                    onCompleteItem: function (item, res) {
                        if (res.result || res.retCode == 0) {
                            options.success && options.success(res);
                            return;
                        }
                        options.fail && options.fail(res);
                    },
                    onErrorItem: function (item,res) {
                        options.fail && options.fail(res);
                    },
                    onWhenAddingFileFailed: function (item,filter) {
                        options.error && options.error(filter.name);
                    }
                });
            };
        }
    ]);
});
